<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\hcbao\Hcbao;
use app\admin\model\jianshebaorecord\Jianshebaorecord;
use app\admin\model\recharge\Recharge;
use app\admin\model\User;
use app\admin\model\withdraw\Withdraw;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }

        $time0 = strtotime(date("Ymd",time()));
        $where['create_time']=['>',$time0];
        //今日总充值额度(USDT)
        $rechargeModel = new Recharge();
        $today_recharge_amount = $rechargeModel->where(['status' => 1,'unit'=>2])->where($where)->sum('amount');
        $all_recharge_amount = $rechargeModel->where(['status' => 1,'unit'=>2])->sum('amount');

        //今日总充值HC
        $all_hc_recharge_amount = $rechargeModel->where(['status' => 1,'unit'=>1])->sum('amount');

        //今日总提现额度
        $withdrawModel = new Withdraw();
        $today_withdraw_amount = $withdrawModel->where(['status' => 1])->where($where)->sum('real_amount');
        $all_withdraw_amount = $withdrawModel->where(['status' => 1])->sum('real_amount');

        //建设宝七天质押数
        $janshebaorecordModel = new Jianshebaorecord();
        $qitian = $janshebaorecordModel->where(['id'=>1])->sum('amount');
        //建设宝15天质押数
        $shiwutian = $janshebaorecordModel->where(['id'=>2])->sum('amount');
        //建设宝30天质押数
        $sanshitian = $janshebaorecordModel->where(['id'=>3])->sum('amount');
        //建设宝90天质押数
        $jiushitian = $janshebaorecordModel->where(['id'=>4])->sum('amount');

        //HC宝总投资额
        $hcbaoModel = new Hcbao();
        $all_hcbao = $hcbaoModel->where([])->sum('amount');

        //待释放HC
        $userModel = new User();
        $hc_balance = $userModel->where([])->sum('hc_balance');

        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            'today_recharge_amount'   => $today_recharge_amount,
            'today_withdraw_amount'   => $today_withdraw_amount,
            'all_recharge_amount'     => $all_recharge_amount,
            'all_withdraw_amount'     => $all_withdraw_amount,
            'all_hc_recharge_amount'  => $all_hc_recharge_amount,
            'qitian'                  => $qitian,
            'shiwutian'               => $shiwutian,
            'sanshitian'              => $sanshitian,
            'jiushitian'              => $jiushitian,
            'hc_balance'              => $hc_balance,
            'all_hcbao'               => $all_hcbao
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}
